<?php
	class Books extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function index($limit = 0){
			$this->load->model('Books_model');
			$result = $this->Books_model->get_list($limit,20);

			//分页设置 
			$this->load->library('pagination');
			$page_config['base_url'] = '/books/index/' ;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 3;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();

			$current_url = '/books';

			$this->smarty->assign("action", '图书库');
			$this->smarty->assign("total_count", $result['count']);
			$this->smarty->assign("name", '');
			$this->smarty->assign("conditions", '');
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("books/list.html");


		}



		function add(){
			$post_data = $this->input->post();
			if($post_data){
				if(isset($post_data['file'])) unset($post_data['file']);
				if(isset($post_data['image'])) $post_data['image'] = get_qiniu_url($post_data['image']);
				$post_data['add_time'] = time();
				if(!isset($post_data['status'])) $post_data['status'] = 0;
				
				//添加数据库
				$this->db->insert("books", $post_data);
				$insert_id = $this->db->insert_id();
				if($insert_id > 0){
 					show_message($_SERVER['HTTP_REFERER']);
 				}else{
 					show_message($_SERVER['HTTP_REFERER'], "操作失败");
 				}

				// print_r($post_data);exit;
			}else{
				//获取习惯
				$this->load->model("Books_model");

				$cate_list = $this->Books_model->get_category_list();
				

				$this->smarty->assign("list", $cate_list);
				$this->smarty->assign("info", array());
				$this->smarty->assign("action", '添加图书');
				$this->smarty->assign("post_url", '/books/add');
				$this->smarty->display("books/save.html");
			}


			
		}




	}